package binary_tree.Leetcode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * 迭代进行中序遍历
 */
public class Num94_InOrderNoRecursion {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> ret=new ArrayList<>();
        if (root==null){
            return ret;
        }
        TreeNode cur=root;
        Deque<TreeNode> stack=new ArrayDeque<>();
        while (cur!=null||!stack.isEmpty()){
            while(cur!=null){
                //不管三七二十一 一路向左走
                stack.push(cur);
                cur=cur.left;
            }
            //此时cur为空说明走到了最左侧
            cur=stack.pop();
            ret.add(cur.val);
            //继续访问右子树
            cur=cur.right;
        }
        return ret;
    }
}
